package com.xmzs.common.oss.core;


import cn.hutool.core.util.IdUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.CannedAccessControlList;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import com.xmzs.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Base64;


/**
 * Cos文件服务
 *
 * @author: wangle
 * @date: 2023/6/5
 */
@Component
public class CosClient {

    /**
     * 存储桶名称
     */
    @Value("${tencent-cos.bucket-name}")
    private String bucketName;

    /**
     * 腾讯云域名
     */
    @Value("${tencent-cos.domain}")
    private String domain;

    /**
     * 腾讯云账号秘钥
     */
    @Value("${tencent-cos.secret-id}")
    private String secretId;

    /**
     * 密码秘钥
     */
    @Value("${tencent-cos.secret-key:}")
    private String secretKey;

    /**
     * 存储桶地区
     */
    @Value("${tencent-cos.region:}")
    private String region;

    /**
     * 上传MultipartFile格式图片
     *
     * @param file
     * @return String
     * @Date 2023/7/14
     **/
    public String uploadFile(MultipartFile file) {
        String filePath = "";
        COSClient cosClient = getCosClient();
        try {
            String originalFilename = file.getOriginalFilename();
            // 获得文件流
            InputStream inputStream = file.getInputStream();
            //设置文件key
            String date = DateUtils.getDate();
            long nextId = IdUtil.getSnowflakeNextId();
            String name = nextId + originalFilename.substring(originalFilename.lastIndexOf("."));
            String folderName = "/StableDiffusion/" + date + "/";
            filePath = folderName + name;
            // 上传文件
            cosClient.putObject(new PutObjectRequest(bucketName, filePath, inputStream, null));
            cosClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cosClient.shutdown();
        }
        return domain + filePath;
    }

    /**
     * 上传Base64格式图片
     *
     * @param base64
     * @return String
     * @Date 2023/7/14
     **/
    public String uploadBase64(String base64) {
        String filePath = "";
        COSClient cosClient = getCosClient();
        byte[] decode = Base64.getDecoder().decode(base64);
        try {
            ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setContentType("image/jpeg");
            //设置文件key
            String date = DateUtils.getDate();
            long nextId = IdUtil.getSnowflakeNextId();
            String folderName = "/StableDiffusion/" + date + "/";
            filePath = folderName + nextId + ".png";
            // 上传文件
            cosClient.putObject(new PutObjectRequest(bucketName, filePath, new ByteArrayInputStream(decode), metadata));
            cosClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cosClient.shutdown();
        }
        return domain + filePath;
    }

    /**
     * 初始化cos对象,配置相关配置信息
     *
     * @param
     * @return COSClient
     * @Date 2023/6/5
     **/
    public COSClient getCosClient() {
        // 初始化用户身份信息
        COSCredentials cred = new BasicCOSCredentials(this.secretId, this.secretKey);
        // 设置区域
        Region region = new Region(this.region);
        ClientConfig clientConfig = new ClientConfig(region);
        // 生成客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        return cosClient;
    }

}
